package com.campus_assistant.controller;

import com.campus_assistant.common.ToVue;
import com.campus_assistant.config.db.DynamicDataSource;
import com.campus_assistant.pojo.db1.Courses;
import com.campus_assistant.pojo.db1.Student;
import com.campus_assistant.service.db1.CoursesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class CoursesController {
    @Autowired
    private CoursesService coursesService;

    @RequestMapping("/selectAllCourses")//查询所有课程信息
    public ToVue selectAllCourses(){
        DynamicDataSource.dbName.set("db1");
        List<Courses> courses = coursesService.selectAll();
        return ToVue.suc(courses);
    }
    @RequestMapping("/selectCoursesByTeacherName")//通过教师名查课程
    public ToVue selectCoursesByTeacherName(@RequestParam("teacherName")String teacherName){
        DynamicDataSource.dbName.set("db1");
        List<Courses> courses = coursesService.getCoursesByTeacherName(teacherName);
        System.out.println(courses);
        return ToVue.suc(courses);
    }
    @RequestMapping("/selectTeacherNameByCourses")//通过课程名查教师
    public ToVue selectTeacherNameByCourses(@RequestParam("coursesName")String coursesName){
        DynamicDataSource.dbName.set("db1");
        List<String>  teacherName= coursesService.getTeacherNameByCoursesName(coursesName);
        System.out.println(teacherName);
        return ToVue.suc(teacherName);
    }
    @RequestMapping("/addCourses")//添加课程1
    public ToVue addCourses(@RequestParam("result")String result){
        DynamicDataSource.dbName.set("db1");
        String[] message = result.split(" ");//根据空格分隔信息
        int cId=1;
        String courseName=message[0];
        String teacherName=message[1];
        Courses courses=new Courses(cId,courseName,teacherName);
        coursesService.addCourses(courses);
        return ToVue.suc(courses);
    }
    @RequestMapping("/addCourses2")//添加课程2
    public ToVue addCourses2(Courses courses){
        DynamicDataSource.dbName.set("db1");
        coursesService.addCourses(courses);
        return ToVue.suc();
    }
    @RequestMapping("/updateCourse")//修改课程信息
    public ToVue updateCourse(@RequestParam("courseName") String courseName,@RequestParam("teacherName") String teacherName){
        DynamicDataSource.dbName.set("db1");

        int id=1;
        Courses courses = new Courses(id, courseName, teacherName);
        int i = coursesService.updateCourses(courses);
        if(i==0){
            return ToVue.fail();
        }else {
            return ToVue.suc();
        }

    }
    @RequestMapping("/updateCourse2")
    public ToVue updateCourse2(@RequestParam("courseName") String courseName) {
        List<Courses> coursesByName = coursesService.getCoursesByName(courseName);
        return ToVue.suc(coursesByName);
    }
    @RequestMapping("/deleteCourse")//删除课程信息
    public ToVue deleteCourse(@RequestParam("courseName") String courseName,@RequestParam("teacherName") String teacherName){
        DynamicDataSource.dbName.set("db1");
        System.out.println(courseName+"and"+teacherName);
        int id=coursesService.getIdByCourseNameAndTeacherName(courseName,teacherName);//获取将要被删除课程的id
        coursesService.deleteCourses(id);
        return ToVue.suc();

    }

    @RequestMapping("/deleteCourse2")//删除课程信息
    public ToVue deleteCourse2(@RequestParam("id") int id){
        DynamicDataSource.dbName.set("db1");
        coursesService.deleteCourses(id);
        return ToVue.suc();

    }
}
